
let cartsto = "cartlist";
var util=require('util.js');
var  del=  function (index,that) {
  
  wx.showModal({
    title: '提示',
    content: '确定删除吗',
    success: function (res) {
      if (res.confirm) {
       // var index = parseInt(e.currentTarget.dataset.id);
        var arr = that.data.cartarr;
        var totalMoney = that.data.totalMoney;
        var buycar_num = that.data.buycar_num;
        if (arr[index].selected) {
          totalMoney -= Number(arr[index].price * arr[index].num);
          buycar_num -= Number(arr[index].num);
        }
        arr.splice(index, 1); 
        var empty=false;
        if (arr.length==0){empty=true}
        that.setData({
          cartarr: arr,
          totalMoney: totalMoney,
          buycar_num: buycar_num,
          empty: empty
        });
        setcartlist(arr);
        
      }
    }
  })
};
var setshopcart=function(i,num,that){
  var cartlist = that.data.cartarr;
  var cart=cartlist[i];
  if (cart.num > 0 && Number(cart.num) + num>0){
    cartlist[i].num = Number( cartlist[i].num )+num;
    var totalMoney = that.data.totalMoney;
    var buycar_num = that.data.buycar_num;
    if (cartlist[i].selected) {
      totalMoney = Number(totalMoney) + cartlist[i].price * num;
      buycar_num = Number(buycar_num) + num
    }   
    that.setData({
      cartarr: cartlist,
      totalMoney: totalMoney,
      buycar_num: buycar_num
    })
    setcartlist(cartlist);
  } else if (Number(cart.num) + num === 0){
    del(i,that);
  }
  
}
var getshopcart=function(){

} 

var foodtocart=function(index,num,that,tindex){
  var carts = that.data.cartarr;
  var foods = that.data.foodarr;
  var food=foods[index];
  var isInCart=false;
  for(var i=0;i<carts.length;i++){
    if (food.id === carts[i].id) {     
      if ((tindex && that.data.tastearr[tindex] == carts[i].taste)
        || !tindex){
        isInCart = true;
        carts[i].num = Number(carts[i].num) + num;       
      }      
    }
    if (carts[i].num ==0){
      carts.splice(i);
    }       
  }
  if (!isInCart){
    var cart = { id: food.id, pic: food.pic, name: food.name, num: num, price: food.price,oprice:food.oprice, selected: true };
    if (tindex && tindex >=0){
      cart.taste=that.data.tastearr[tindex]
    }
    carts.push(cart);
  }
  foods[index].num = Number(foods[index].num) + num;
  
  console.log(that.data);
  that.setData({
    foodarr:foods,
    cartarr:carts,
    shopcartnum: Number(that.data.shopcartnum) + num
  })
  setcartlist(carts);
  
}
var setTabBadge = function (index, num) {
  if (num > 0) {
    wx.setTabBarBadge({
      index: index,
      text: num + ""
    })
  } else {
    wx.removeTabBarBadge({ index });
  }
}
var setcartlist = function (cart) {
  wx.setStorageSync("cartsto", cart);
  var num=0;
  for(var i=0;i<cart.length;i++){
    num += cart[i].num;
  }
  setTabBadge(1,num);
}
var getcartlist=function(){
  return wx.getStorageSync("cartsto");
}



var foodlist = [];
var cartlist = [];
var cartnum = 0;
var amount = 0;

var setFoodList = function (foodarr) {
  if (foodarr) {
    foodlist = foodarr;
  }
  wx.setStorageSync("foodlist", foodlist);
}
var setCartList = function (cartarr) {
  if (cartarr) {
    cartlist = cartarr
  }
  wx.setStorageSync("cartsto", cartlist);
}
var getFoodList = function () {
  return wx.getStorageSync("foodlist");
}
var getCartList = function () {
  return wx.getStorageSync("cartsto");
}
var getCartNum = function () {
  return wx.getStorageSync("cartnum");
}
var getCartAmount = function () {
  return wx.getStorageSync("cartamount");
}


var addTocart = function (id, num, callback) {
 // if (id==0) return ;
  cartnum = 0;
  amount = 0;
  if (!cartnum) cartnum = 0;
  if (!amount) amount = 0;
  foodlist = getFoodList();
  cartlist = getCartList();
  if (!cartlist) cartlist = [];
  var incart = false;
  var removeindex=-1;
  for (var i = 0; i < cartlist.length; i++) {
    cartnum += cartlist[i].num;
    amount += cartlist[i].num * cartlist[i].price;
    if (id == cartlist[i].id) {
      cartlist[i].num = cartlist[i].num + num;
      if (cartlist[i].num==0){
        removeindex=i;
      }
      incart = true;
    }
  }
  if (removeindex>=0){
    cartlist.splice(removeindex, 1);
  }
  
  for (var i = 0; i < foodlist.length; i++) {
    if (foodlist[i].id == id) {
      var food = foodlist[i];
      foodlist[i].num = foodlist[i].num + num;
      if (foodlist[i].num<0) {
        util.showinfo("购买数量不能小于0");
        return;
      }  
      if (!incart) {
        var cart = { id: food.id, pic: food.pic, name: food.name, num: num, price: food.price, oprice: food.oprice, selected: true };
        cartlist.push(cart);
      }
      cartnum += num;
      amount += food.price * num;
    }
  }
  wx.setStorageSync("cartnum", cartnum);
  wx.setStorageSync("cartamount", amount);
  setFoodList(foodlist);
  setCartList(cartlist);
  setTabBadge(1, cartnum);
  if (callback) callback();
}



module.exports = {
  setshopcart: setshopcart,
  del: del,
  foodtocart: foodtocart,
  setTabBadge: setTabBadge,
  setcartlist: setcartlist,
  getcartlist: getcartlist,

  addTocart: addTocart,
  setCartList: setCartList,
  setFoodList: setFoodList,
  getFoodList: getFoodList,
  getCartList: getCartList,
  getCartNum: getCartNum,
  getCartAmount: getCartAmount
}

